package com.ideaplugin.datamanager.controller;

import cn.dev33.satoken.stp.StpUtil;
import com.ideaplugin.datamanager.common.SwaggerScan;
import com.ideaplugin.datamanager.common.UserInfoHolder;
import com.ideaplugin.datamanager.model.common.BaseResult;
import com.ideaplugin.datamanager.model.common.PlainResult;
import com.ideaplugin.datamanager.model.common.UserInfo;
import com.ideaplugin.datamanager.model.request.ChangePasswordParam;
import com.ideaplugin.datamanager.model.request.LoginUserParam;
import com.ideaplugin.datamanager.model.response.LoginResult;
import com.ideaplugin.datamanager.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created on 2023/6/25.
 *
 * @author zhaojb
 */
@RestController
@RequestMapping("/user")
@SwaggerScan
public class LoginController {
    @Autowired
    private UserService userService;

    @PostMapping("/login")
    public PlainResult<LoginResult> login(@RequestBody LoginUserParam loginUserParam) {
        final boolean login = userService.login(loginUserParam);
        return new PlainResult<>(new LoginResult(login, StpUtil.getTokenValue()));
    }

    @GetMapping("/getUserInfo")
    public PlainResult<UserInfo> getLoginUserInfo() {
        return new PlainResult<>(UserInfoHolder.getUserInfo());
    }

    @GetMapping("/logout")
    public BaseResult logout() {
        StpUtil.logout();
        return new BaseResult();
    }

    @PostMapping("/changePassword")
    public PlainResult<Boolean> changePassword(@RequestBody ChangePasswordParam changePasswordParam) {
        final boolean result = userService.changePassword(changePasswordParam);
        return new PlainResult<>(result);
    }
}
